package nl.nickthissen.remindme.data;

import com.activeandroid.Model;

import java.util.ArrayList;

/**
 * Created by Nick on 13-6-13.
 */
public class Linq
{
    public static <T extends Model> T find(ArrayList<T> items, final long id)
    {
        ArrayList<T> matches = where(items, new Predicate<T>()
        {
            @Override
            public boolean apply(T in)
            {
                return in.getId().equals(id);
            }
        });
        return first(matches);
    }

    public static <T> ArrayList<T> where(ArrayList<T> items, Predicate<T> predicate)
    {
        ArrayList<T> matches = new ArrayList<T>();
        for (T item : items)
        {
            if (predicate.apply(item)) matches.add(item);
        }
        return matches;
    }

    public static <T> T first(ArrayList<T> items)
    {
        if (items.size() > 0) return items.get(0);
        return null;
    }

    public static <T> T last(ArrayList<T> items)
    {
        int size = items.size();
        if (size > 0) return items.get(size-1);
        return null;
    }

    public interface Predicate<T>
    {
        boolean apply(T in);
    }
}
